Ext.Loader.setConfig({
    enabled: true
});

Ext.tip.QuickTipManager.init();  // enable tooltips

Ext.application({
    requires: [
        'Ext.Loader', 
        'Ext.container.Viewport', 
        'Intranet.Desktop.view.Login'
    ],
    
    name: 'Intranet',

    appFolder: 'app/modules',
    
	
    controllers: [
        'Intranet.Desktop.controller.MainScreen',
        'Intranet.Desktop.controller.Toolbox',
        'Intranet.Desktop.controller.Store',
        'Intranet.Desktop.controller.InstalledModules'
    ],
    
    launch: function() {
        
        myApp = this;
        
        
        
        Ext.Ajax.request({
            url: '/src/public/user/auth/isAuthenticated',
            success: function(response) {
                var data = Ext.decode(response.responseText);
                
                if(data.value === 'true')
                { 
                    Ext.Ajax.request({
                        url: '/src/public/module/index/getExtController',

                        success: function(response) {
                            var data = Ext.decode(response.responseText);

                            if(data.success === 'true')
                            {
                                var controllers = data.controllers;

                                var c;

                                for(var i = 0; i < controllers.length; i++)
                                {
                                    c = myApp.getController(controllers[i]);
                                    c.init();
                                }

                                Ext.create('Intranet.Desktop.view.MainScreen');
                                //loadGeneralModules();
                            }
                            else
                            {
                                Ext.Msg.alert('Error', 'Error loading ext controller from database!');
                            }

                        }
                    });
 
                }
                else
                {
                    var loginForm = Ext.create('Intranet.Desktop.view.Login');
        
                    Ext.create('Ext.window.Window', {
                                    id: 'login-window',
                                    title: 'Login',
                                    height: 140,
                                    width: 300,
                                    layout: 'fit',
                                    items: [loginForm]
                    }).show();
                }
                   
            }
        });
        
                
    }
});
